<?php
class ControllerAffiliatePayment extends FrontController {

	public function index() {
		if (!$this->affiliate->isLogged()) {
			$this->session->data['redirect'] = US('affiliate/payment');
			$this->redirect(US('affiliate/login'));
		}

		$this->document->setTitle($this->_['heading_title']);

		M('affiliate/affiliate');

		if ($this->request->isPost()) {
			$this->model_affiliate_affiliate->editPayment($this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(US('affiliate/account'));
		}

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_account'],
			'href'      => US('affiliate/account'),
        	'separator' => $this->_['text_separator']
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_payment'],
			'href'      => US('affiliate/payment'),
        	'separator' => $this->_['text_separator']
      	);

		if (!$this->request->isPost()) {
			$affiliate_info = $this->model_affiliate_affiliate->getAffiliate($this->affiliate->getId());
		}

		if (!empty($affiliate_info)) {
			$this->data['tax'] = $affiliate_info['tax'];
			$this->data['payment'] = $affiliate_info['payment'];
			$this->data['cheque'] = $affiliate_info['cheque'];
			$this->data['paypal'] = $affiliate_info['paypal'];
			$this->data['bank_name'] = $affiliate_info['bank_name'];
			$this->data['bank_branch_number'] = $affiliate_info['bank_branch_number'];
			$this->data['bank_swift_code'] = $affiliate_info['bank_swift_code'];
			$this->data['bank_account_name'] = $affiliate_info['bank_account_name'];
			$this->data['bank_account_number'] = $affiliate_info['bank_account_number'];
		} else {
			$this->data['tax'] = P('tax');
			$this->data['payment'] = P('payment', 'cheque');
			$this->data['cheque'] = P('cheque');
			$this->data['paypal'] = P('paypal');
			$this->data['bank_name'] = P('bank_name');
			$this->data['bank_branch_number'] = P('bank_branch_number');
			$this->data['bank_swift_code'] = P('bank_swift_code');
			$this->data['bank_account_name'] = P('bank_account_name');
			$this->data['bank_account_number'] = P('bank_account_number');
		}

		$this->display('affiliate/payment.tpl');
	}
}
?>